package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
        //创建list集合
        List<JSONObject> list = new ArrayList<>();
        //查询所以视图数据
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
        //一级分类ID分组去重
        Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //遍历map
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            //获取数据
            Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
            //获取key
            Long category1Id = entry.getKey();
            //获取value
            List<BaseCategoryView> baseCategoryViewList1 = entry.getValue();
            //创建一级分类对象
            JSONObject category1 = new JSONObject();
            //放入一级分类对象
            category1.put("categoryId", category1Id);
            category1.put("categoryName", baseCategoryViewList1.get(0).getCategory1Name());
            //以二级分类ID分组去重
            Map<Long, List<BaseCategoryView>> map1 = baseCategoryViewList1.stream()
                    .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
            //声明二级分类集合
            List<JSONObject> categoryChildList = new ArrayList<>();
            while (iterator1.hasNext()) {
                //创建二级分类对象
                JSONObject category2 = new JSONObject();

                Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
                //获取key
                Long category2Id = entry1.getKey();
                //获取value
                List<BaseCategoryView> baseCategoryViewList2 = entry1.getValue();
                //放入二级分类对象
                category2.put("categoryId", category2Id);
                category2.put("categoryName", baseCategoryViewList2.get(0).getCategory2Name());
                //获取三级分类数据
                List<JSONObject> category3ChildList = baseCategoryViewList2.stream().map(baseCategoryView -> {
                    JSONObject category3 = new JSONObject();
                    category3.put("categoryId", baseCategoryView.getCategory3Id());
                    category3.put("categoryName", baseCategoryView.getCategory3Name());
                    return category3;
                }).collect(Collectors.toList());
                //添加三级分类集合
                category2.put("categoryChild", category3ChildList);
                categoryChildList.add(category2);
            }
            //添加二级分类集合
            category1.put("categoryChild", categoryChildList);
            //添加一级分类对象到集合
            list.add(category1);
        }
        return list;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.selectAttribute(category1Id);
    }
}

